﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using System.Collections.Generic;
namespace WPFExcelReport
{
    /// <summary>
    /// Interaction logic for EditEventWindow.xaml
    /// </summary>
    public partial class TWAB45
    {
        #region ==KGNET===========
        public static readonly DependencyProperty KGNETProperty = DependencyProperty.Register("KGNET",
            typeof(double), typeof(TWAB45),
            new FrameworkPropertyMetadata(0.0, FrameworkPropertyMetadataOptions.BindsTwoWayByDefault, OnKGNETChanged));
        public double KGNET
        {
            get
            {
                return (double)GetValue(KGNETProperty);
            }
            set
            {
                SetValue(KGNETProperty, value);
                OnKGNETChg();
              
            }
        }
        private static void OnKGNETChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            var src = sender as TWAB45;
            if (src != null)
            {
                src.KGNET = (double)e.NewValue;
            }
        }
        public virtual void OnKGNETChg()
        {
            if (MV == null) return;
            TMV.KGNET = KGNET;
        }
        #endregion

        public TWAB45()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MESSAGES += "VWABPNEW;";
            MESSAGES += "LOADABP;RESETPROD;";
            InitializeComponent();
            //POSTYP = "VW ";
        }

        public override void Window_Loaded(object sender, RoutedEventArgs e)
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            if (IsLoaded)
                return;
            base.Window_Loaded(sender, e);
        }

        public override void vwMaster_CurrentChanged(object sender, EventArgs e)
        {
            base.vwMaster_CurrentChanged(sender, e);
        }

        #region====WorkFlow========
        MessageInfo Lastmsg
        {
            get
            {
                return TMV.Lastmsg;
            }
            set
            {
                TMV.Lastmsg = value;
            }
        }
         
        public override void OnWorkflowIDChange(MessageEventArgs e)
        {
            IsBusy = true;
            base.OnWorkflowIDChange(e);
            MessageInfo msg = e.Info;
            string task = msg.MSGTYPE;
            switch (task)
            {
                case "VWABPNEW":
                    if (Lastmsg != null)
                    {
                        MessageBox.Show("请先完成前一个订单创建");
                        //return;
                    }
                    RULEKEY = "";//先装载规则
                    RULEKEY = msg.ORDER;
                    Lastmsg = msg;
                    ARTST = msg.WAKZ;
                    ART = msg.ART;
                    VAR = msg.VAR;
                    FARBE = msg.FARBE;
                    PRART = msg.WAKZ+ART + VAR + FARBE;
                    PM["ORDER"] = msg.ORDER;
                    PM["WAKZ"] = msg.WAKZ;
                    PM["ART"] = msg.ART;
                    PM["VAR"] = msg.VAR;
                    PM["FARBE"] = msg.FARBE;
                    PM["BESCHICH"] = msg.BESCHICH.PadRight(10);
                    PM["DESS"] = msg.DESS.PadRight(10);
                    PM["COLORIT"] = msg.COLORIT.PadRight(10);
                    PM["GROE"] = msg.COLORIT.PadRight(7);
                    PM["MAKE2ORD="] = MAKE2ORD;
                    IsOK = false;
                    TMV.VWABPNEW(PM, ref IsOK);
                    IsABPCreated = IsOK;
                    if (IsABPCreated)
                    {
                        ORDERNUM = msg.ORDER;
                    }
                    IsShowAB = false;
                    IsShowAB = true;
                    break;
            }
            IsBusy = false;
        }
        #endregion

        public override void OnAutoChg(bool auto)
        {
            if (!auto)
                return;
            if (dwRule == null)
            {
            }
            base.OnAutoChg(auto);
        }
        public override void OnFLOWChg(string value)
        {
            if (!IsEdit)
                return;
            if (dwMaster == null)
                return;
            if (FLOW == "")
                return;
            TMV.ABPANRKR = FLOW.Substring(0, 2);
            TMV.ABPALFDNR = FLOW.Substring(2, 6);
            TMV.UABP19(dwMaster);
            PM["ORDERNUM"] = TMV.ABPAUFKRS + TMV.ABPAUFLFD + TMV.ABPAUFPOS;
            PM["AUFTYP"] = "AB45";
            TMV.VABF.ABFNEW(PM);
            TMV.VPR.UpdateICC_PRULES(dwRule, 1);
            base.OnFLOWChg(value);
        }
        private void btnPrePlan_Click(object sender, RoutedEventArgs e)
        {
            IsABPCreated = false;
            if (Lastmsg == null)
            {
                Lastmsg = new MessageInfo();
                Lastmsg.ORDER = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString();
                Lastmsg.WAKZ = dwAB["ABPWAKZ"].ToString();
                Lastmsg.ART = dwAB["ABPART"].ToString();
                Lastmsg.VAR = dwAB["ABPVAR"].ToString();
                Lastmsg.FARBE = dwAB["ABPFARBE"].ToString();
                Lastmsg.BESCHICH = dwAB["ABPBESCHICH"].ToString();
                Lastmsg.DESS = dwAB["ABPDESS"].ToString();
                Lastmsg.COLORIT = dwAB["ABPCOLORIT"].ToString();
                Lastmsg.GROE = dwAB["ABPGROE"].ToString();
            }
            PM["ORDER"] = Lastmsg.ORDER;
            PM["WAKZ"] = Lastmsg.WAKZ;
            PM["ART"] = Lastmsg.ART;
            PM["VAR"] = Lastmsg.VAR;
            PM["FARBE"] = Lastmsg.FARBE;
            PM["BESCHICH"] = Lastmsg.BESCHICH.PadRight(10);
            PM["DESS"] = Lastmsg.DESS.PadRight(10);
            PM["COLORIT"] = Lastmsg.COLORIT.PadRight(10);
            PM["GROE"] = Lastmsg.GROE.PadRight(7);
            PM["MAKE2ORD="] = MAKE2ORD;
            IsOK = false;
            TMV.VWABPNEW(PM, ref IsOK);
            IsABPCreated = IsOK;
            if (IsABPCreated&&dwAB!=null)
            {
                string key = dwAB["ABPAUFKRS"].ToString() + dwAB["ABPAUFLFD"].ToString() + dwAB["ABPAUFPOS"].ToString() + dwAB["ABPPOSTYP"].ToString();
                ORDERNUM = "";
                ORDERNUM = key;
            }
            
        }
        public override void OnEXAUFChg(string vlaue)
        {
            TBMain.Clear();
            if (!IsCanLoad)
                return;
            if (EXAUF.Length < 11)
                return;
            ORDERNUM = "";
            KRS = EXAUF.Substring(0, 2);
            LFD = EXAUF.Substring(2, 6);
            POS = EXAUF.Substring(8, 3);
            ORDERNUM = KRS + LFD + POS + POSTYP;
            base.OnEXAUFChg(vlaue);
        }
        public override void LOADBYAUF()
        {
            if (!IsCanLoad)
                return;
            base.LOADBYAUF();
        }
    }
    partial class VABPOS
    {
        public void VWABPNEW(Dictionary<string, object> pm, ref bool IsABPCreated)
        {
            try
            {
                if (dwRule["UNDES"].ToString().Trim().Length < 8)
                    return;
                if (PRLO.Length < 2)
                    return;
                if (PRLB.Length < 2)
                    return;
                PM["VKAUF"] = dwRule["P4"];
                PM["KRS"] = KRS;
                APZANRKRS = dwRule["UNDES"].ToString().Substring(0, 2);
                APZALFDNR = dwRule["UNDES"].ToString().Substring(2, 6);
                if (APZALFDNR.Trim().Length < 6)
                {
                    throw new Exception("请选择流程");
                }
                #region====Rules======== 
                double _R01 = 0;  
                double  LEADTIME = 0;
                double BATHNUM = 1;
                double _JWASTGE = 0;
                if (dwRule != null)
                {
                    _R01 = Convert.ToDouble(R01) / 100;//浆纱伸长                   
                    BATHNUM = Convert.ToDouble(JNUM);//浆缸数
                    _JWASTGE = Convert.ToDouble(JWASTGE);//固定50米    
                    LEADTIME = Convert.ToDouble(LEADTIME);//生产周期
                }
                

                double ABPSOLLMENGE5 = Convert.ToDouble(QTYEND) * KGNET /1000;       
                double _JNUM = Convert.ToDouble(JNUM);
                if (_JNUM <= 0)
                    _JNUM = 1;
                double WarpLeng = 0;
                 WarpLeng = Math.Ceiling((QTYEND + F01) / (1 + R01));//经轴长度
                #endregion

                #region=======dwData==========
                DataRow dwABP = TBMain.NewRow();
                SetUpNewRow(ref dwABP, null);
                ShareedABP(dwABP, pm);
                dwABP["ABPAENDZL"] = 4;
                dwABP["ABPMANDANT"] = app.Mandant;
                dwABP["ABPAUFART"] = "VW";
           
              
                dwABP["ABPFA"] = 0;
                dwABP["ABPGA"] = 0;
                dwABP["ABPQA"] = 0;
                dwABP["ABPREGEL"] = "00000000";
                dwABP["ABPLAEAENDA"] = 0;
                dwABP["ABPZLO"] = PRLO;
                dwABP["ABPZLB"] = PRLB;
                dwABP["ABPBLO"] = PRLO; 
           
                dwABP["ABPSME"] = "1";
                dwABP["ABPSOLLMENGE1"] = QTYEND * 100;
                dwABP["ABPSOLLMENGE2"] = WarpLeng * 100;
                dwABP["ABPSOLLMENGE3"] = 0;
                dwABP["ABPSOLLMENGE4"] = 0;
                dwABP["ABPSOLLMENGE5"] = ABPSOLLMENGE5;
                dwABP["ABPSOLLMENGE6"] = ABPSOLLMENGE5;
                dwABP["ABPSOLLMENGE7"] = _JNUM * 100;
                dwABP["ABPSOLLMENGE8"] = _JNUM * 100;
                dwABP["ABPSZUSATZKZ1"] = " ";
                dwABP["ABPANZSTK"] = 0;
                dwABP["ABPZUGETGES"] = 0;
                dwABP["ABPSTKLG"] = 0;
                dwABP["ABPKETTAUFT"] = " ";
                dwABP["ABPDMART"] = "    ";
                string vkp = dwRule["P5"].ToString().PadRight(11).Substring(0, 11);
                dwABP["ABPKDPARTIE"] = vkp.Substring(0, 2) + vkp.Substring(4, 4) + "-" + vkp.Substring(8, 3);//销售单号
                #endregion

                Insert(dwABP);
                IsABPCreated = true;
                VPR.UpdateICC_PRULES(dwRule, 1);

                PreparePM(dwABP);  
     
                VPAR.NABPR(PM); 
                VABF.ABFNEW(PM);
                PM["WAKZ"] = "W";
                PM["VKAUF"] = dwRule["P4"];
                PM["VKPAUF"] = dwRule["P5"];
                PM["ABAAUFTYP"] = "V";
                PM["JNUM"] = _JNUM;
                PM["ABPSOLLMENGE5"] = ABPSOLLMENGE5;
                VMU.MUNEW(PM); 

                VBD.NEWBD320(PM);

                VBB.NEWVWBB(PM);
                PM["GASA"] = "723";
                VGD.NEWGD723(PM);
                Lastmsg = null;
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
                MessageBox.Show(ex.Message);
            }
            finally
            {

            }
        }
    }
}
